﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using Pb;

/// <summary>
/// 设置管理脚本
/// </summary>

public class SettingModel : MonoBehaviour {
    public UILabel IDLabel;//ID
    //按钮
    public GameObject CloseBtn;//关闭设置窗口
    public GameObject QuitGameBtn;//退出游戏
    public GameObject SwitchBtn;//切换账号
    public GameObject CloseMusicBtn;//点击关闭音乐
    public GameObject OpenMusicBtn;//点击打开音乐
    public GameObject CloseSoundBtn;//关闭音效
    public GameObject OpenSoundBtn;//打开音效
    public GameObject SettingPanel;
    private EginUser user = EginUser.Instance;
    public  UISprite Avatar;//头像
    public UITexture wxAvatar;//微信头像

    private void Awake()
    {
        UIEventListener.Get(CloseBtn).onClick = ButtonClick;
        UIEventListener.Get(QuitGameBtn).onClick = ButtonClick;
        UIEventListener.Get(SwitchBtn).onClick = ButtonClick;
        UIEventListener.Get(CloseMusicBtn).onClick = ButtonClick;
        UIEventListener.Get(OpenMusicBtn).onClick = ButtonClick;
        UIEventListener.Get(CloseSoundBtn).onClick = ButtonClick;
        UIEventListener.Get(OpenSoundBtn).onClick = ButtonClick;
    }

    void Start()
    {
        if (user.bgMusic)
        {
            CloseMusicBtn.SetActive(true);
            OpenMusicBtn.SetActive(false);
        }
        else
        {
            OpenMusicBtn.SetActive(true);
            CloseMusicBtn.SetActive(false);
        }
        if (user.effectMusic)
        {
            CloseSoundBtn.SetActive(true);
            OpenSoundBtn.SetActive(false);
        }
        else
        {
            OpenSoundBtn.SetActive(true);
            CloseSoundBtn.SetActive(false);
        }

        Avatar.spriteName = user.avatarId.ToString();
        IDLabel.text = "ID:" + user.Uid;

        //是微信登陆
        if(user.loginControl == 1){
            //StartCoroutine(GetWWWFACE(user.userUrl));
            //WXAvatarManager._instance.AddWxAvatar(int.Parse(user.Uid),wxAvatar.mainTexture,user.userUrl);
            wxAvatar.mainTexture = WXAvatarManager._instance.wxAvatarDic[int.Parse(user.Uid)];
            wxAvatar.gameObject.SetActive(true);
            Avatar.gameObject.SetActive(false);
            Debug.Log(123);
        }
        //手机登陆
        else
        {

        }
              
    }

    void ButtonClick(GameObject button)
    {                
        if (button.name == "CloseBtn")
        {
            Destroy(SettingPanel);
        }
        else if (button.name == "CloseMusicBtn")
        {
            CloseMusicBtn.SetActive(false);
            OpenMusicBtn.SetActive(true);
            SoundModel._BGVolume = 0;
            user.bgMusic = false;
        }
        else if (button.name == "OpenMusicBtn")
        {
            OpenMusicBtn.SetActive(false);
            CloseMusicBtn.SetActive(true);
            SoundModel._BGVolume = 0.6f;
            user.bgMusic = true;
        }
        else if (button.name == "CloseSoundBtn")
        {
            CloseSoundBtn.SetActive(false);
            OpenSoundBtn.SetActive(true);
            SoundModel.EffectVolume = 0;
            user.effectMusic = false;
        }
        else if (button.name == "OpenSoundBtn")
        {
            OpenSoundBtn.SetActive(false);
            CloseSoundBtn.SetActive(true);
            SoundModel.EffectVolume = 0.6f;
            user.effectMusic = true;
        }
        else if (button.name == "SwitchBtn")
        {
            SwitchAccountRequest msg = new SwitchAccountRequest { };
            SocketManager socketManager = SocketManager.Instance;
            socketManager.SendProtobuf1(msg, MsgId.SwitchAccountRequest);

            SceneManager.LoadSceneAsync(ScenePath.Login);
        }
        else if (button.name == "QUitBtn")
        {
            Application.Quit();
        }
    }

    IEnumerator GetWWWFACE(string url)
    {

        if (url != "")
        {
            WWW www = new WWW(url);


            yield return www;
            if (www.isDone)
            {
                if (www.error == null) { }
                wxAvatar.gameObject.SetActive(true);
                wxAvatar.mainTexture = www.texture;

            }
            else
            {

            }
            www.Dispose();
        }

    }
}
